//
//  UIView+PopupProtocol.m
//  NewiOSProject
//
//  Created by li on 2023/9/6.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "UIView+PopupProtocol.h"

@implementation UIView (PopupProtocol)

- (void)show:(BOOL)toggle target:(UIView *)rootView edgeInsets:(UIEdgeInsets)edgeInsets {
    
    if (toggle) {
        if (self.superview == nil) {
            [self popup:rootView edgeInsets:edgeInsets];
        }
    }else {
        
        if (self.superview != nil) {
            [self dismissPopup];
        }
    }
}

- (void)popup:(UIView *)rootView edgeInsets: (UIEdgeInsets)edgeInsets {
    
    self.translatesAutoresizingMaskIntoConstraints = NO;
    self.translucentView.translatesAutoresizingMaskIntoConstraints = NO;
    self.translucentView.clipsToBounds = YES;
    self.translucentView.alpha = 0;
    
    [rootView addSubview: self.translucentView];
    
    [NSLayoutConstraint activateConstraints:@[
        [self.translucentView.topAnchor constraintEqualToAnchor:rootView.topAnchor constant:edgeInsets.top],
        [self.translucentView.leftAnchor constraintEqualToAnchor:rootView.leftAnchor constant:edgeInsets.left],
        [self.translucentView.bottomAnchor constraintEqualToAnchor:rootView.bottomAnchor constant:-edgeInsets.bottom],
        [self.translucentView.rightAnchor constraintEqualToAnchor:rootView.rightAnchor constant: -edgeInsets.right],
    ]];
    
    [self.translucentView addSubview:self];
    [self layoutIfNeeded];
    
    [self showLayout:NO];
    [UIView animateWithDuration:0.25 animations:^{
        [self showLayout:YES];
        self.translucentView.alpha = 1;
        self.alpha = 1;
    }];
}

- (void)showLayout:(BOOL)hasShowed {
    if (self.modalStyle == PopupViewModalStyleFromTop) {
        self.transform = CGAffineTransformMakeTranslation(0, hasShowed ? self.frame.size.height : 0);
        if (!hasShowed) {
            [NSLayoutConstraint activateConstraints:@[
                [self.bottomAnchor constraintEqualToAnchor: self.translucentView.topAnchor constant:0],
                [self.centerXAnchor constraintEqualToAnchor: self.translucentView.centerXAnchor constant:0],
            ]];
        }
    }else if (self.modalStyle == PopupViewModalStyleFromRight) {
        self.transform = CGAffineTransformMakeTranslation(hasShowed ? -self.frame.size.width : 0, 0);
        
        if (!hasShowed) {
            [NSLayoutConstraint activateConstraints:@[
                [self.leftAnchor constraintEqualToAnchor: self.translucentView.rightAnchor constant:0],
                [self.centerYAnchor constraintEqualToAnchor: self.translucentView.centerYAnchor constant:0],
            ]];
        }
    }else if (self.modalStyle == PopupViewModalStyleFromLeft) {
        self.transform = CGAffineTransformMakeTranslation(hasShowed ? self.frame.size.width : 0, 0);
        
        if (!hasShowed) {
            [NSLayoutConstraint activateConstraints:@[
                [self.rightAnchor constraintEqualToAnchor: self.translucentView.leftAnchor constant:0],
                [self.centerYAnchor constraintEqualToAnchor: self.translucentView.centerYAnchor constant:0],
            ]];
        }
    }else if (self.modalStyle == PopupViewModalStyleFromBottom) {
        self.transform = CGAffineTransformMakeTranslation(0, hasShowed ? 0 : self.frame.size.height);
        
        if (!hasShowed) {
            [NSLayoutConstraint activateConstraints:@[
                [self.bottomAnchor constraintEqualToAnchor: self.translucentView.bottomAnchor constant:0],
                [self.centerXAnchor constraintEqualToAnchor: self.translucentView.centerXAnchor constant:0],
            ]];
        }
    }else if (self.modalStyle == PopupViewModalStyleCenterScale) {
        self.transform = CGAffineTransformMakeTranslation(hasShowed ? 1 : 0.1, hasShowed ? 1 : 0.1);
        
        if (!hasShowed) {
            [NSLayoutConstraint activateConstraints:@[
                [self.centerXAnchor constraintEqualToAnchor: self.translucentView.centerXAnchor constant:0],
                [self.centerYAnchor constraintEqualToAnchor: self.translucentView.centerYAnchor constant:0],
            ]];
        }
    }
}

- (void)dismissPopup {
    [UIView animateWithDuration:0.2 animations:^{
        self.translucentView.alpha = 0;
        [self dismissAnimation];
    } completion:^(BOOL finished) {
        if (self.dismissCompletion) {
            self.dismissCompletion();
        }
    }];
}

- (void)dismissAnimation {
    
    if (self.modalStyle == PopupViewModalStyleCenterScale) {
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
    }else if (self.modalStyle == PopupViewModalStyleFromBottom) {
        self.transform = CGAffineTransformMakeTranslation(0, self.frame.size.height);
    }else {
        self.transform = CGAffineTransformMakeTranslation(0, 0);
    }
}

- (UIView *)defaultTranslucentView {
    
    PopupTranslucentView *v = [PopupTranslucentView buttonWithType:(UIButtonTypeCustom)];
    v.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    @weakify(self)
    v.clickAction = ^{
        @strongify(self)
        [self dismissPopup];
    };
    
    return v;
}

- (void)addTapGestuer: (void(^)(UIView *view))action {
    @weakify(self)
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        action(self);
    }];
    
    [self addGestureRecognizer:tap];
}


- (UIView *)then:(void (^)(UIView * _Nonnull))configure {
    
    if (configure != nil) {
        configure(self);
    }
    return self;
}
@end
